package com.xxx.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.blog.domain.Article;
import com.xxx.blog.domain.GuiDang;
import com.xxx.blog.query.ArticleQuery;
import com.xxx.blog.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
@Api(tags = "文章内容接口类")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @GetMapping
    @ApiOperation(value = "查询所有文章")
    public R<List<Article>> findAll() {
        List<Article> list = articleService.findAll();
        return R.ok(list);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单篇文章")
    public R<Article> findOne(@PathVariable Long id) {
        Article article = articleService.findOne(id);
        return R.ok(article);
    }

    @PostMapping
    @ApiOperation(value = "分页条件查询")
    public R<PageInfo<Article>> queryPage(@RequestBody ArticleQuery articleQuery) {
        List<Article> list = articleService.queryPage(articleQuery);
        return R.ok(new PageInfo<Article>(list));
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改")
    public R<Void> queryPage(@RequestBody Article article) {
        try {
            if (article.getId() == null) {
                articleService.addArticle(article);
            } else {
                articleService.updateArticle(article);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除文章")
    public R<Void> deleteArticle(@PathVariable Long id) {
        try {
            articleService.deleteArticle(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @PatchMapping
    @ApiOperation(value = "批量删除文章")
    public R<Void> batchArticle(@RequestBody Long[] ids) {
        try {
            articleService.batchArticle(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @GetMapping("/articleTimeStatistics")
    @ApiOperation(value = "归档博客文章")
    public R<List<GuiDang>> findArticleTimeStatistics() {
        return R.ok(articleService.findArticleTimeStatistics());
    }

    @GetMapping("/relateArticles/{id}")
    @ApiOperation(value = "查询相关文章")
    public R<List<Article>> getRelateArticles(@PathVariable Long id) {
        List<Article> list = articleService.getRelateArticles(id);
        return R.ok(list);
    }

    @GetMapping("/archiveDetail/{yearMonth}")
    @ApiOperation(value = "查询相关文章")
    public R<List<Article>> archiveDetail(@PathVariable("yearMonth") String yearMonth)
    {
        return R.ok(articleService.findArticleByYearMonth(yearMonth));
    }
}